﻿Imports System.Data
Imports System.Data.SqlClient
Imports BEGestionProyecto
Imports DLGestionProyecto

Public Class BLGPHoraRegistro
#Region "CONSULTAR"
    '*********************************************************
    'Funcion que Lista los Proyectos
    'fecha crea         fecha modifica         autor
    '01/02/2012         01/02/2012              JKP
    '*********************************************************
    Public Function fuBLListarHoraRegistroxFechas(ByVal piPersonalID As Integer, ByVal pfFechRegistroI As Date, ByVal pfFechRegistroF As Date) As DataTable
        Try
            Dim lDt As New DataTable
            Dim lBL As New DLGestionProyecto.DLGPHoraRegistro
            lDt = lBL.fuDLListarHoraRegistroxFechas(piPersonalID, pfFechRegistroI, pfFechRegistroF)
            Return lDt
        Catch ex As Exception
            Return Nothing
            Throw New ArgumentException("Origen : fuBLListarHoraRegistroxFechas " & vbCrLf & _
                                        "Descripcion : " & ex.Message)
        End Try
    End Function
    '*********************************************************
    'Funcion que Lista los Proyectos
    'fecha crea         fecha modifica         autor
    '01/02/2012         01/02/2012              JKP
    '*********************************************************
    Public Function fuBLBuscarHoraRegistrox(ByVal piRegistrohoraID As Integer) As DataTable
        Try
            Dim lDt As New DataTable
            Dim lBL As New DLGestionProyecto.DLGPHoraRegistro
            lDt = lBL.fuDLBuscarHoraRegistrox(piRegistrohoraID)
            Return lDt
        Catch ex As Exception
            Throw New ArgumentException("Origen : fuBLBuscarHoraRegistrox " & vbCrLf & _
                                        "Descripcion : " & ex.Message)
        End Try
    End Function


    '*********************************************************
    'Funcion que Lista los Proyectos
    'fecha crea         fecha modifica         autor
    '01/02/2012         01/02/2012              JKP
    '*********************************************************
    Public Function fuBLVerficarCruceHoras(ByVal pfHorini As DateTime, ByVal pfHorhorFin As DateTime, _
            ByVal pDt As DataTable) As String
        Try
            Dim ls As String = ""
            For Each lrow In pDt.Rows
                If Format(lrow("horini"), "HH:mm") < Format(pfHorhorFin, "HH:mm") And _
                    Format(lrow("horini"), "HH:mm") >= Format(pfHorini, "HH:mm") Then
                    ls &= String.Format("* Existe un cruce de fechas : {0} {1}</BR>", Format(lrow("horini"), "HH:mm"), Format(lrow("horhorFin"), "HH:mm"))
                ElseIf Format(lrow("horhorFin"), "HH:mm") > Format(pfHorini, "HH:mm") And _
                    Format(lrow("horhorFin"), "HH:mm") <= Format(pfHorhorFin, "HH:mm") Then
                    ls &= String.Format("* Existe un cruce de fechas : {0} {1}</BR>", Format(lrow("horini"), "HH:mm"), Format(lrow("horhorFin"), "HH:mm"))
                ElseIf Format(lrow("horini"), "HH:mm") <= Format(pfHorini, "HH:mm") And _
                    Format(lrow("horhorFin"), "HH:mm") >= Format(pfHorhorFin, "HH:mm") Then
                    ls &= String.Format("* Existe un cruce de fechas : {0} {1}</BR>", Format(lrow("horini"), "HH:mm"), Format(lrow("horhorFin"), "HH:mm"))
                ElseIf Format(lrow("horini"), "HH:mm") >= Format(pfHorini, "HH:mm") And _
                    Format(lrow("horhorFin"), "HH:mm") <= Format(pfHorhorFin, "HH:mm") Then
                    ls &= "* Existe un cruce de fechas : " & Format(lrow("horini"), "HH:mm") & " " & Format(lrow("horhorFin"), "HH:mm") & "</BR>"
                End If
            Next
            Return ls
        Catch ex As Exception
            Throw New ArgumentException("Origen : fuBLVerficarCruceHoras " & vbCrLf & _
                                        "Descripcion : " & ex.Message)
        End Try
    End Function

    Function fuBLValidarAlGuardar(ByVal pfHorini As DateTime, ByVal pfHorhorFin As DateTime, _
        ByVal psHorcodPersona As String, ByVal pfHorfecConsumida As DateTime, ByVal piHorcodProyectoActividad As Integer, _
        ByVal ldHorcanHoras As Decimal, ByVal piHorcodReghora As Integer, _
        ByVal pbEsEditar As Boolean, ByVal pLstError As List(Of String)) As Integer
        Dim ls As String = ""
        Dim lBL As New DLGestionProyecto.DLGPHoraRegistro
        Dim lDs As New DataSet
        lDs = lBL.fuDProcesoVerificarAlGuardar(psHorcodPersona, pfHorfecConsumida, piHorcodProyectoActividad, piHorcodReghora)

        'If pbEsEditar = True Then
        '    Dim li As Integer
        '    For Each lrow In lDs.Tables(0).Rows
        '        If lrow("horcodReghora") = piHorcodReghora Then
        '            lDs.Tables(0).Rows.RemoveAt(li)
        '            Exit For
        '        End If
        '        li += 1
        '    Next
        '    If lDs.Tables(1).Rows.Count > 0 Then

        '    End If

        'End If

        If lDs.Tables(0).Rows.Count > 0 Then
            ls = fuBLVerficarCruceHoras(pfHorini, pfHorhorFin, lDs.Tables(0))
            If ls.Trim.Length > 0 Then
                pLstError.Add(ls)
                pLstError.Add("2-CRITICO")
                Return 1
            End If
        End If
        'If lDs.Tables(1).Rows.Count > 0 Then
        '    If ldHorcanHoras + IIf(IsDBNull(lDs.Tables(1).Rows(0)("CNTHORASCONSUMIDAS")), 0, lDs.Tables(1).Rows(0)("CNTHORASCONSUMIDAS")) _
        '            > lDs.Tables(1).Rows(0)("actnroHoras") Then
        '        ls = "* La cantidad de Horas de la acumulada excede a la cantidad de horas estimada"
        '        pLstError.Add(ls)
        '        pLstError.Add("3-CONPREGUNTA")
        '        Return 2
        '    End If
        'End If
        '4-INFORMATIVO
    End Function



    Public Function fuBLListarProyectoxPersona(ByVal piPepPersonalID As Integer) As DataTable
        Try
            Dim lDt As New DataTable
            Dim lBL As New DLGestionProyecto.DLGPHoraRegistro
            lDt = lBL.fuDLListarProyectoxPersona(piPepPersonalID)
            Return lDt
        Catch ex As Exception
            Throw New ArgumentException("Origen : fuBLListarProyectoActividadxPersona " & vbCrLf & _
                                        "Descripcion : " & ex.Message)
        End Try
    End Function

    Public Function fuBLListarActividadxPersona(ByVal piPersonalID As Integer, ByVal piProyectoID As Integer) As DataTable
        Try
            Dim lDt As New DataTable
            Dim lBL As New DLGestionProyecto.DLGPHoraRegistro
            lDt = lBL.fuDLListarActividadxPersona(piPersonalID, piProyectoID)
            Return lDt
        Catch ex As Exception
            Throw New ArgumentException("Origen : fuBLListarActividadxPersona " & vbCrLf & _
                                        "Descripcion : " & ex.Message)
        End Try
    End Function


#End Region
#Region "ACTUALIZAR"
        '*********************************************************
        'Funcion que Agrega Proyectos
        'fecha crea         fecha modifica         autor
        '01/02/2012         01/02/2012              JKP
        '*********************************************************
    Public Function fuBLInsertarHoraRegistro(ByVal pBE As BEGPHoraRegistro) As Boolean
        Try
            Dim lBL As New DLGestionProyecto.DLGPHoraRegistro
            If lBL.fuDLInsertarHoraRegistro(pBE) Then
                Return True
            End If

        Catch ex As Exception
            Throw New ArgumentException("Origen : fuBLInsertarHoraRegistro " & vbCrLf & _
                                        "Descripcion : " & ex.Message)

        End Try
    End Function
    '*********************************************************
    'Funcion que Agrega Proyectos
    'fecha crea         fecha modifica         autor
    '01/02/2012         01/02/2012              JKP
    '*********************************************************
    Public Function fuBLActualizarHoraRegistro(ByVal pBE As BEGPHoraRegistro) As Boolean
        Try
            Dim lBL As New DLGestionProyecto.DLGPHoraRegistro
            If lBL.fuDLActualizarHoraRegistro(pBE) Then
                Return True
            End If
        Catch ex As Exception
            Throw New ArgumentException("Origen : fuBLActualizarHoraRegistro " & vbCrLf & _
                                        "Descripcion : " & ex.Message)

        End Try
    End Function
    '*********************************************************
    'Funcion que Agrega Proyectos
    'fecha crea         fecha modifica         autor
    '01/02/2012         01/02/2012              JKP
    '*********************************************************
    Public Function fuBLEliminarHoraRegistro(ByVal piHorcodReghora As Integer, ByVal psHorcodUserModifica As String) As Boolean
        Try

            Dim lBL As New DLGestionProyecto.DLGPHoraRegistro
            If lBL.fuDLEliminarHoraRegistro(piHorcodReghora, psHorcodUserModifica) Then
                Return True
            End If
        Catch ex As Exception
            Throw New ArgumentException("Origen : fuBLEliminarHoraRegistro " & vbCrLf & _
                                        "Descripcion : " & ex.Message)

        End Try
    End Function

#End Region
End Class
